package com.yzd.learnjava.concurrency;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import com.yzd.learnjava.util.Print;

public class Interrupting2 {
	public static void main(String[] args) throws InterruptedException {
		Thread t = new Thread(new Blocked2());
		t.start();
		TimeUnit.SECONDS.sleep(1);
		Print.print("Issuing t.interrupt()");
		t.interrupt();
	}
}

class BlockedMutex{
	
	private ReentrantLock lock = new ReentrantLock();
	public BlockedMutex() {
		lock.lock();
	}
	
	public void f() {
		try {
			lock.lockInterruptibly();
			Print.print("lock acquired in f()");
		} catch (InterruptedException e) {
			Print.print("Interrupted from lock acquisition in f()");
		}
	}
}

class Blocked2 implements Runnable{
	BlockedMutex blocked = new BlockedMutex();
	public void run() {
		Print.print("Waiting for f() in BlockedMutex");
		blocked.f();
		Print.print("Broken out of call");
	}
}
